package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.ISaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.GoodsUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 当销售单产生后，订单会进入待出库状态，需要仓库管理员进行出库操作 服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements ISaleService {
    @Autowired
    SaleMapper saleMapper;
    @Autowired
    SaleGoodsMapper saleGoodsMapper;
    @Autowired
    SalePacketMapper salePacketMapper;
    @Autowired
    SalePacketGoodsMapper salePacketGoodsMapper;
    @Autowired
    SaleOrderMapper saleOrderMapper;
    @Autowired
    SaleOrderGoodsMapper saleOrderGoodsMapper;
    @Autowired
    SaleOrderPacketMapper saleOrderPacketMapper;
    @Autowired
    ArrearsWaitMapper arrearsWaitMapper;
    @Autowired
    GoodsSnMapper goodsSnMapper;
    @Autowired
    GoodsSnLogMapper goodsSnLogMapper;
    @Autowired
    PacketMapper packetMapper;
    @Autowired
    GoodsCostMapper goodsCostMapper;
    @Autowired
    HouseWaitOutMapper houseWaitOutMapper;
    @Autowired
    HouseWaitOutGoodsMapper houseWaitOutGoodsMapper;
    @Autowired
    HouseWaitOutPacketMapper houseWaitOutPacketMapper;
    @Autowired
    HouseWaitOutPacketGoodsMapper houseWaitOutPacketGoodsMapper;
    @Autowired
    GoodsStockMapper goodsStockMapper;
    @Autowired
    UserLastRetailMapper userLastRetailMapper;
    @Autowired
    BorrowOutMapper borrowOutMapper;
    @Autowired
    BorrowOutGoodsMapper borrowOutGoodsMapper;
    @Autowired
    SettAccountMapper settAccountMapper;

    @Override
    public boolean checkClose(Sale sale) {
        return saleMapper.selectCount(new QueryWrapper<Sale>().eq("id",sale.getId()).eq("close",true)) > 0;
    }

    @Override
    public boolean checkNo(String no, String companyId) {
        return saleMapper.selectCount(new QueryWrapper<Sale>().eq("company_id",companyId).eq("no",no)) > 0;
    }


    @Override
    public List<SaleVo> listVo(String companyId,
                               Integer page,
                               Integer limit,
                               Boolean close,
                               String search,
                               String no,
                               String goodsName,
                               String remark,
                               String storeId,
                               String houseId,
                               String settAccountId,
                               String custom,
                               Date deliveryDate1,
                               Date deliveryDate2,
                               Date makeTime1,
                               Date makeTime2,
                               String userStoreId,
                               String userId,
                               Date date1,
                               Date date2,
                               Date date,
                               String makeUserId,
                               String customId,
                               Integer status) {
        return saleMapper.listVo(companyId,
                Utils.genPage(page,limit),
                limit,
                close,
                search,
                no,
                goodsName,
                remark,
                storeId,
                houseId,
                settAccountId,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                userStoreId,
                userId,
                date1,
                date2,
                date,
                makeUserId,
                customId,
                status);
    }

    @Override
    public long count(String companyId,
                      Boolean close,
                      String search,
                      String no,
                      String goodsName,
                      String remark,
                      String storeId,
                      String houseId,
                      String settAccountId,
                      String custom,
                      Date deliveryDate1,
                      Date deliveryDate2,
                      Date makeTime1,
                      Date makeTime2,
                      String userStoreId,
                      String userId,
                      Date date1,
                      Date date2,
                      Date date,
                      String makeUserId,
                      String customId,
                      Integer status){
        return saleMapper.count(companyId,
                close,
                search,
                no,
                goodsName,
                remark,
                storeId,
                houseId,
                settAccountId,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                userStoreId,
                userId,
                date1,
                date2,
                date,
                makeUserId,
                customId,
                status);
    }

    @Override
    public List<SaleVo> listVoForTicket(String companyId,
                                        Integer page,
                                        Integer limit,
                                        Integer ticket,
                                        String search,
                                        String no,
                                        String goodsName,
                                        String remark,
                                        String storeId,
                                        String houseId,
                                        String settAccountId,
                                        String custom,
                                        Date deliveryDate1,
                                        Date deliveryDate2,
                                        Date makeTime1,
                                        Date makeTime2,
                                        String userStoreId,
                                        String userId,
                                        Date date1,
                                        Date date2,
                                        String makeUserId) {
        return saleMapper.listVoForTicket(companyId,
                Utils.genPage(page,limit),
                limit,
                ticket,
                search,
                no,
                goodsName,
                remark,
                storeId,
                houseId,
                settAccountId,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId);
    }

    @Override
    public long countForTicket(String companyId,
                               Integer ticket,
                               String search,
                               String no,
                               String goodsName,
                               String remark,
                               String storeId,
                               String houseId,
                               String settAccountId,
                               String custom,
                               Date deliveryDate1,
                               Date deliveryDate2,
                               Date makeTime1,
                               Date makeTime2,
                               String userStoreId,
                               String userId,
                               Date date1,
                               Date date2,
                               String makeUserId) {
        return saleMapper.countForTicket(companyId,
                ticket,
                search,
                no,
                goodsName,
                remark,
                storeId,
                houseId,
                settAccountId,
                custom,
                deliveryDate1,
                deliveryDate2,
                makeTime1,
                makeTime2,
                userStoreId,
                userId,
                date1,
                date2,
                makeUserId);
    }

    @Override
    public List<SaleVo> listVoForSelect(String companyId,
                                        Integer page,
                                        Integer limit,
                                        String search,
                                        String storeId,
                                        String userStoreId,
                                        String userId) {
        return saleMapper.listVoForSelect(companyId,
                Utils.genPage(page,limit),
                limit,
                search,
                storeId,
                userStoreId,
                userId);
    }

    @Override
    public Long countForSelect(String companyId,
                               String search,
                               String storeId,
                               String userStoreId,
                               String userId) {
        return saleMapper.countForSelect(companyId,
                search,
                storeId,
                userStoreId,
                userId);
    }

    @Override
    public SaleVo getVoById(String id) {
        return saleMapper.getVoById(id);
    }

    @Override
    public List<SaleVo> listVoByIds(List<String> ids) {
        return saleMapper.listVoByIds(ids);
    }

    @Override
    public List<SaleVo> listVoBySaleOrder(String saleOrderId) {
        return saleMapper.listVoBySaleOrder(saleOrderId);
    }

    @Override
    public SaleVo getLastRetailByUserId(String userId) {
        return saleMapper.getLastRetailByUserId(userId);
    }

    @Override
    public List<SaleVo> listVoByBorrowOut(String borrowOutId) {
        return saleMapper.listVoByBorrowOut(borrowOutId);
    }

    @Override
    public boolean checkTicket(List<String> ids) {
        return saleMapper.checkTicket(ids) > 0;
    }

    @Override
    public void batchTicket(List<String> ids) {
        saleMapper.batchTicket(ids);
    }


    @Override
    public void insert(BusinessSet businessSet, Sale sale,
                       List<SaleGoods> saleGoodsList, List<GoodsSn> goodsSnList,List<GoodsSnLog> goodsSnLogList,
                       List<SalePacket> salePacketList, List<SalePacketGoods> salePacketGoodsList) {

        saleMapper.insert(sale);
        if (!Utils.isEmpty(saleGoodsList)) {
            saleGoodsMapper.insertBatch(saleGoodsList);
        }

        if (!Utils.isEmpty(salePacketList)) {
            salePacketMapper.insertBatch(salePacketList);
            salePacketGoodsMapper.insertBatch(salePacketGoodsList);
        }

        SaleVo saleVo = saleMapper.getVoById(sale.getId());

        //账户余额+
        settAccountMapper.addBalance(saleVo.getRealAmt(), saleVo.getSettAccountId());

        //收款单处理
        processArrearsWait(1, saleVo);

        //处理关联的销售订单
        processSaleOrder(1,saleVo);

        if (!Utils.isEmpty(saleVo.getBorrowOutId())) {
            //由借出单转销售的话，就直接处理借出单就行，
            processBorrowOut(1, saleVo);
            if (!Utils.isEmpty(goodsSnLogList)) {
                goodsSnLogMapper.insertBatch(goodsSnLogList);
            }
        } else {
            if (!PdosUtils.houseInOut(businessSet)) {
                //未启用出入库管理，直接添加库存
                processGoodsStock(1, saleVo);
                if (!Utils.isEmpty(goodsSnList)) {
                    goodsSnMapper.outByList(saleVo.getHouseId(), goodsSnList);
                    goodsSnLogMapper.insertBatch(goodsSnLogList);
                }
            } else {
                //启用库存管理，则添加待出库单数据
                processHouseWaitOut(1, saleVo);
            }
        }
    }

    @Override
    public void retailInsert(Sale sale,
                             List<SaleGoods> saleGoodsList, List<GoodsSn> goodsSnList, List<GoodsSnLog> goodsSnLogList,
                             List<SalePacket> salePacketList, List<SalePacketGoods> salePacketGoodsList) {
        saleMapper.insert(sale);
        if (!Utils.isEmpty(saleGoodsList)) {
            saleGoodsMapper.insertBatch(saleGoodsList);
        }

        if (!Utils.isEmpty(salePacketList)) {
            salePacketMapper.insertBatch(salePacketList);
            salePacketGoodsMapper.insertBatch(salePacketGoodsList);
        }

        SaleVo saleVo = saleMapper.getVoById(sale.getId());

        //账户余额+
        settAccountMapper.addBalance(saleVo.getRealAmt(), saleVo.getSettAccountId());

        processGoodsStock(1, saleVo);
        if (!Utils.isEmpty(goodsSnList)) {
            goodsSnMapper.outByList(saleVo.getHouseId(), goodsSnList);
            goodsSnLogMapper.insertBatch(goodsSnLogList);
        }

        //用户最后零售单表处理
        UserLastRetail userLastRetail = new UserLastRetail();
        userLastRetail.setId(UuidUtils.getUUID());
        userLastRetail.setUserId(sale.getUserId());
        userLastRetail.setSaleId(sale.getId());
        userLastRetailMapper.insertOrUpdate(userLastRetail);
    }

    @Override
    public void close(Sale sale, BusinessSet businessSet) {
        SaleVo saleVo = saleMapper.getVoById(sale.getId());

        //余额-
        settAccountMapper.addBalance(saleVo.getRealAmt().negate(), saleVo.getSettAccountId());

        processSaleOrder(2,saleVo);

        //处理应付欠款
        processArrearsWait(2,saleVo);

        //同时还要删除零售收银的最后一笔单据
        userLastRetailMapper.delete(new QueryWrapper<UserLastRetail>().eq("sale_id",saleVo.getId()));

        if (!Utils.isEmpty(saleVo.getBorrowOutId())) {
            processBorrowOut(2,saleVo);
            //改变该销售单对应的sn日志的borrow状态为0即 为关闭
            goodsSnLogMapper.updateBorrowBySaleId(saleVo.getId(),0);
        } else {
            //直接出库的销售单，则需要去除相应的库存和sn。
            if (saleVo.getStatus() == 1) {
                processGoodsStock(2, saleVo);

                //关闭进货单需要sn号需要处与在库状态
                //关闭进货单需要sn号需要处与在库状态
                List<GoodsSn> list = PdosUtils.genGoodsSnList(saleVo.getSaleGoodsVoList());
                if (!Utils.isEmpty(list)) {
                    goodsSnMapper.inByList(saleVo.getHouseId(), list);
                }
            } else {
                //更新待出库单的关联单据的关闭状态
                processHouseWaitOut(2, saleVo);
            }
        }

        Sale temp = new Sale();
        temp.setId(sale.getId());
        temp.setClose(true);
        saleMapper.updateById(temp);
    }

    private void processBorrowOut(int type, SaleVo saleVo){
        //借出转销售的商品不会有套餐，所以不需要考虑套餐部分
        if (Utils.isEmpty(saleVo.getBorrowOutId())) {
            return;
        }

        List<BorrowOutGoods> borrowOutGoodsList = new ArrayList<>();
        for (SaleGoodsVo vo :saleVo.getSaleGoodsVoList()){
            BorrowOutGoods entity = new BorrowOutGoods();
            entity.setId(vo.getBorrowOutGoodsId());
            entity.setBorrowOutId(saleVo.getBorrowOutId());
            entity.setGoodsId(vo.getGoodsId());
            entity.setNumber(vo.getNumber());
            entity.setSaleNumber(type==1 ? vo.getNumber():vo.getNumber().negate());
            entity.setHasNumber(new BigDecimal(0));
            entity.setWaitNumber(type==1 ? vo.getNumber().negate():vo.getNumber());
            entity.setUnitId(vo.getUnitId());
            entity.setTimes(vo.getTimes());
            entity.setPrice(vo.getPrice());
            entity.setAmount(vo.getAmount());
            entity.setCompanyId(saleVo.getCompanyId());
            borrowOutGoodsList.add(entity);
        }
        borrowOutGoodsMapper.insertBatch(borrowOutGoodsList);

        BorrowOut temp = new BorrowOut();
        temp.setId(saleVo.getBorrowOutId());

        long totalWaitInNumber = borrowOutGoodsMapper.countWaitNumber(saleVo.getBorrowOutId());
        long number = borrowOutGoodsMapper.countNumber(saleVo.getBorrowOutId());
        if(totalWaitInNumber > 0) {

            if (number == totalWaitInNumber){//未归还
                temp.setOutStatus(1);
            } else {//部分归还
                temp.setOutStatus(2);
            }
        } else {
            //全部归还
            temp.setOutStatus(3);
        }
        borrowOutMapper.updateById(temp);
    }


    /**
     * 处理待出库单
     * @param type 1新增销售单 2作废销售单
     * @param saleVo
     */
    private void processHouseWaitOut(int type, SaleVo saleVo) {
        if (type == 2) {
            //更新关联待出库单的关闭状态
            HouseWaitOut houseWaitOut = new HouseWaitOut();
            houseWaitOut.setClose(true);
            houseWaitOutMapper.update(houseWaitOut, new QueryWrapper<HouseWaitOut>().eq("sale_id", saleVo.getId()));
            return;
        }

        HouseWaitOut houseWaitOut = new HouseWaitOut();
        houseWaitOut.setId(UuidUtils.getUUID());
        houseWaitOut.setSaleId(saleVo.getId());
        houseWaitOut.setHouseId(saleVo.getHouseId());
        houseWaitOut.setClose(false);
        houseWaitOut.setStatus(1);//待出库
        houseWaitOut.setType(2);//设置类型为销售
        houseWaitOut.setCompanyId(saleVo.getCompanyId());
        houseWaitOut.setTime(saleVo.getTime());
        houseWaitOutMapper.insert(houseWaitOut);

        if (!Utils.isEmpty(saleVo.getSaleGoodsVoList())) {
            List<HouseWaitOutGoods> houseWaitOutGoodsList = new ArrayList<>();
            for (SaleGoodsVo vo : saleVo.getSaleGoodsVoList()) {
                HouseWaitOutGoods houseWaitOutGoods = new HouseWaitOutGoods();
                houseWaitOutGoods.setId(UuidUtils.getUUID());
                houseWaitOutGoods.setHouseWaitOutId(houseWaitOut.getId());
                houseWaitOutGoods.setSaleGoodsId(vo.getId());
                houseWaitOutGoods.setGoodsId(vo.getGoodsId());
                houseWaitOutGoods.setHasOutNumber(new BigDecimal(0));
                houseWaitOutGoods.setWaitOutNumber(vo.getNumber());
                houseWaitOutGoods.setNumber(vo.getNumber());
                houseWaitOutGoods.setUnitId(vo.getUnitId());
                houseWaitOutGoods.setTimes(vo.getTimes());
                houseWaitOutGoods.setCompanyId(saleVo.getCompanyId());
                houseWaitOutGoodsList.add(houseWaitOutGoods);
            }
            houseWaitOutGoodsMapper.insertBatch(houseWaitOutGoodsList);
        }

        if (!Utils.isEmpty(saleVo.getSalePacketVoList())) {
            List<HouseWaitOutPacket> houseWaitOutPacketList = new ArrayList<>();
            List<HouseWaitOutPacketGoods> houseWaitOutPacketGoodsList = new ArrayList<>();
            for (SalePacketVo vo : saleVo.getSalePacketVoList()) {
                HouseWaitOutPacket houseWaitOutPacket = new HouseWaitOutPacket();
                houseWaitOutPacket.setId(UuidUtils.getUUID());
                houseWaitOutPacket.setHouseWaitOutId(houseWaitOut.getId());
                houseWaitOutPacket.setSalePacketId(vo.getId());
                houseWaitOutPacket.setPacketId(vo.getPacketId());
                houseWaitOutPacket.setPacketName(vo.getPacketName());
                houseWaitOutPacket.setPacketNo(vo.getPacketNo());
                houseWaitOutPacket.setHasOutNumber(new BigDecimal(0));
                houseWaitOutPacket.setWaitOutNumber(vo.getNumber());
                houseWaitOutPacket.setNumber(vo.getNumber());
                houseWaitOutPacket.setCompanyId(saleVo.getCompanyId());
                houseWaitOutPacketList.add(houseWaitOutPacket);

                for (SalePacketGoodsVo packetGoodsVo : vo.getPacketGoodsVoList()) {
                    HouseWaitOutPacketGoods entity = new HouseWaitOutPacketGoods();
                    entity.setId(UuidUtils.getUUID());
                    entity.setHouseWaitOutId(houseWaitOut.getId());
                    entity.setHouseWaitOutPacketId(houseWaitOutPacket.getId());
                    entity.setPacketId(packetGoodsVo.getPacketId());
                    entity.setGoodsId(packetGoodsVo.getGoodsId());
                    entity.setNumber(packetGoodsVo.getNumber());
                    entity.setUnitId(packetGoodsVo.getUnitId());
                    entity.setTimes(packetGoodsVo.getTimes());
                    entity.setPrice(packetGoodsVo.getPrice());
                    entity.setAmount(packetGoodsVo.getAmount());
                    entity.setCompanyId(saleVo.getCompanyId());
                    houseWaitOutPacketGoodsList.add(entity);
                }
            }
            houseWaitOutPacketMapper.insertBatch(houseWaitOutPacketList);
            houseWaitOutPacketGoodsMapper.insertBatch(houseWaitOutPacketGoodsList);
        }
    }


    /**
     *
     * 处理库存
     * @param type 1新增销售单 2作废销售单
     * @param saleVo
     */
    private void processGoodsStock(int type, SaleVo saleVo) {
        GoodsUtil goodsUtil = new GoodsUtil(saleVo.getHouseId(), saleVo.getSaleGoodsVoList(), saleVo.getSalePacketVoList());
        List<GoodsStock> goodsStockList = new ArrayList<>();
        for ( Map.Entry<String,BigDecimal> entry: goodsUtil.getAllGoodsNumberMap().entrySet()) {
            String goodsId = entry.getKey();
            BigDecimal number = entry.getValue();
            if (type == 1) {
                //销售是减少库存的动作，所以需要减少库存
                number = number.negate();
            }

            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setId(UuidUtils.getUUID());
            goodsStock.setGoodsId(goodsId);
            goodsStock.setHouseId(saleVo.getHouseId());
            goodsStock.setNum(number);
            goodsStock.setCompanyId(saleVo.getCompanyId());
            goodsStock.setTime(new Date());
            goodsStockList.add(goodsStock);
        }

        goodsStockMapper.insertBatch(goodsStockList);
    }

    /**
     * 处理关联的销售订单
     * @param type 1新增销售单 2作废销售单
     * @param saleVo
     */
    private void processSaleOrder(int type, SaleVo saleVo) {
        if (Utils.isEmpty(saleVo.getSaleOrderId())) {
            return;
        }

        if(!Utils.isEmpty(saleVo.getSaleGoodsVoList())) {
            //从进货订单发起的进货，需要同步处理进货订单
            List<SaleOrderGoods> saleOrderGoodsList = new ArrayList<>();
            for (SaleGoodsVo saleGoodsVo : saleVo.getSaleGoodsVoList()) {
                if (!Utils.isEmpty(saleGoodsVo.getSaleOrderGoodsId())) {
                    SaleOrderGoods vo = new SaleOrderGoods();
                    vo.setId(saleGoodsVo.getSaleOrderGoodsId());
                    vo.setHasNumber(type == 1 ? saleGoodsVo.getNumber() : saleGoodsVo.getNumber().negate());
                    vo.setWaitNumber(type == 1 ? saleGoodsVo.getNumber().negate() : saleGoodsVo.getNumber());
                    vo.setGoodsId(saleGoodsVo.getGoodsId());
                    vo.setSaleOrderId(saleVo.getSaleOrderId());

                    vo.setNumber(saleGoodsVo.getNumber());
                    vo.setPrice(saleGoodsVo.getPrice());
                    vo.setAmount(saleGoodsVo.getAmount());
                    vo.setTimes(saleGoodsVo.getTimes());
                    vo.setUnitId(saleGoodsVo.getUnitId());
                    vo.setRemark(saleGoodsVo.getRemark());
                    vo.setCompanyId(saleVo.getCompanyId());
                    saleOrderGoodsList.add(vo);
                }
            }
            saleOrderGoodsMapper.insertBatch(saleOrderGoodsList);
        }

        if(!Utils.isEmpty(saleVo.getSalePacketVoList())) {
            //从进货订单发起的进货，需要同步处理进货订单
            List<SaleOrderPacket> saleOrderPacketList = new ArrayList<>();
            for (SalePacketVo salePacketVo : saleVo.getSalePacketVoList()) {
                if (!Utils.isEmpty(salePacketVo.getSaleOrderPacketId())) {
                    SaleOrderPacket vo = new SaleOrderPacket();
                    vo.setId(salePacketVo.getSaleOrderPacketId());
                    vo.setSaleOrderId(saleVo.getSaleOrderId());
                    vo.setPacketId(salePacketVo.getPacketId());
                    vo.setPacketNo(salePacketVo.getPacketNo());
                    vo.setPacketName(salePacketVo.getPacketName());
                    vo.setHasNumber(type == 1 ? salePacketVo.getNumber() : salePacketVo.getNumber().negate());
                    vo.setWaitNumber(type == 1 ? salePacketVo.getNumber().negate() : salePacketVo.getNumber());

                    vo.setNumber(salePacketVo.getNumber());
                    vo.setPrice(salePacketVo.getPrice());
                    vo.setAmount(salePacketVo.getAmount());
                    vo.setRemark(salePacketVo.getRemark());
                    vo.setCompanyId(saleVo.getCompanyId());
                    saleOrderPacketList.add(vo);
                }
            }
            saleOrderPacketMapper.insertBatch(saleOrderPacketList);
        }


        SaleOrder temp = new SaleOrder();
        temp.setId(saleVo.getSaleOrderId());

        long totalWaitInNumber = saleOrderGoodsMapper.countWaitNumber(saleVo.getSaleOrderId()) + saleOrderPacketMapper.countWaitNumber(saleVo.getSaleOrderId());
        long number = saleOrderGoodsMapper.countNumber(saleVo.getSaleOrderId()) + saleOrderPacketMapper.countNumber(saleVo.getSaleOrderId());
        temp.setStatus(2);
        if(totalWaitInNumber > 0) {
            if (number == totalWaitInNumber){
                temp.setTransferStatus(1);
            } else {
                temp.setTransferStatus(2);
            }
            temp.setClose(false);
        } else {
            //全部入库
            temp.setTransferStatus(3);
            temp.setStatus(3);
            temp.setClose(true);
        }
        saleOrderMapper.updateById(temp);

    }

    /**
     * 处理应收款
     * @param type 1新增销售单 2作废销售单
     * @param saleVo
     */
    private void processArrearsWait(int type, SaleVo saleVo) {
        if (saleVo.getTotalAmt().compareTo(saleVo.getRealAmt()) == 1) {
            //总计金额大于实收金额，此时产生应收款
            ArrearsWait dbArrearsWait = arrearsWaitMapper.selectOne(new QueryWrapper<ArrearsWait>().eq("custom_id",saleVo.getCustomId()).eq("company_id", saleVo.getCompanyId()));
            if (dbArrearsWait == null) {
                if (type == 1) {
                    ArrearsWait arrearsWait = new ArrearsWait();
                    arrearsWait.setId(UuidUtils.getUUID());
                    arrearsWait.setCustomId(saleVo.getCustomId());
                    arrearsWait.setCompanyId(saleVo.getCompanyId());
                    BigDecimal total = saleVo.getTotalAmt().subtract(saleVo.getRealAmt());
                    arrearsWait.setTotal(total);
                    arrearsWait.setHasIn(new BigDecimal("0"));
                    arrearsWait.setWaitIn(arrearsWait.getTotal());
                    arrearsWait.setDiscount(new BigDecimal("0"));
                    arrearsWaitMapper.insert(arrearsWait);
                }
            } else {
                BigDecimal total = saleVo.getTotalAmt().subtract(saleVo.getRealAmt());
                if (type == 1) {
                    dbArrearsWait.setTotal(total.add(dbArrearsWait.getTotal()));
                    dbArrearsWait.setWaitIn(total.add(dbArrearsWait.getWaitIn()));
                } else {
                    dbArrearsWait.setTotal(dbArrearsWait.getTotal().subtract(total));
                    dbArrearsWait.setWaitIn(dbArrearsWait.getWaitIn().subtract(total));
                }

                arrearsWaitMapper.updateById(dbArrearsWait);
            }
        }
    }
}
